﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace HRdex_Final.Employee
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Button1.Enabled = false;
                lblConfirmation.Visible = false;

                string EmployeeID = Request.QueryString["EmployeeID"];
            
                if (EmployeeID == null)
                {
                    txtAddress.Enabled = true;
                    txtContactNo.Enabled = true;
                    txtEmail.Enabled = true;
                        txtEmpNum.Enabled = true;
                        txtFName.Enabled = true;
                        txtLName.Enabled = true;
                        txtMName.Enabled = true;
                        ddlCollege.Enabled = true;
                        ddlDepartment.Enabled = true;
                        ddlPosition.Enabled = true;
                        ddlUserLevel.Enabled = true;
                        txtBday.Enabled = true;
                        lblduplicate.Visible = false;
                }
                //Panel1.Visible = true;
                //pOutput.Visible = false;
                else
                {
                Select();
                }
            }
            catch (Exception)
            {
                
                
            }



        }

        protected void txtSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string EmployeeID = Request.QueryString["EmployeeID"];

                if (txtEmpNum.Text != EmployeeID)
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    conn.Open();

                    SqlCommand cmd = new SqlCommand("Insert Into  formEmployees (EmployeeID, LastName, FirstName, MiddleName, catPositionsID, CollegeID, DeptID, UserLevelID, Password, Address, Birthday, Email, ContactNo)" +
                        "Values (@empNo, @lname, @fname, @mname, @position, @college, @dept, @ulevel, @pass, @address, @birthday, @email, @contactno)", conn);
                    Stream fs = fuPicture.PostedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);

                    string filePath = fuPicture.PostedFile.FileName;
                    string fileName = Path.GetFileName(filePath);


                    string a = "adu";
                    cmd.Parameters.Add("@empNo", SqlDbType.Int).Value = txtEmpNum.Text.ToUpper();
                    cmd.Parameters.Add("@lname", SqlDbType.VarChar).Value = txtLName.Text.ToUpper();
                    cmd.Parameters.Add("@fname", SqlDbType.VarChar).Value = txtFName.Text.ToUpper();
                    cmd.Parameters.Add("@mname", SqlDbType.VarChar).Value = txtMName.Text.ToUpper();
                    cmd.Parameters.Add("@position", SqlDbType.Int).Value = ddlPosition.SelectedValue;
                    cmd.Parameters.Add("@college", SqlDbType.Int).Value = ddlCollege.SelectedValue;
                    cmd.Parameters.Add("@dept", SqlDbType.Int).Value = ddlDepartment.SelectedValue;
                    cmd.Parameters.Add("@ulevel", SqlDbType.Int).Value = ddlUserLevel.SelectedValue;
                    cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = a;
                    cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = txtAddress.Text.ToUpper();
                    cmd.Parameters.Add("@birthday", SqlDbType.VarChar).Value = txtBday.Text;
                    cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
                    cmd.Parameters.Add("@contactno", SqlDbType.VarChar).Value = txtContactNo.Text;
                    //cmd.Parameters.Add("@pic", SqlDbType.Image).Value = bytes;
                    //cmd.Parameters.Add("@picName", SqlDbType.VarChar).Value = fileName;

                    SqlCommand cmd122 = new SqlCommand(" SELECT [EmployeeID] FROM formEmployees WHERE EmployeeID ="+txtEmpNum.Text, conn);
                    SqlDataAdapter sqlda2 = new SqlDataAdapter(cmd122);
                    DataTable dt122 = new DataTable("dt122");
                    sqlda2.Fill(dt122);
                    if (dt122.Rows.Count > 0)
                    {
                        lblduplicate.Visible = true;
                        lblduplicate.Text = "Employee ID already exists";
                    }
                    else 
                    {
                        cmd.ExecuteNonQuery();
                        lblConfirmation.Visible = true;
                        lblConfirmation.Text = "You successfully add new Employee";
                       
                    }

                    Button1.Enabled = true;
                    txtSubmit.Enabled = false;
                    conn.Close();


                }
                else
                {
                    Edit();
                    lblConfirmation.Visible = true;
                    lblConfirmation.Text = "You successfully edit the Employees Information";
                }
                txtAddress.Enabled = false;
                txtCancel.Enabled = false;
                txtContactNo.Enabled = false;
                txtEmail.Enabled = false;
                txtEmpNum.Enabled = false;
                txtFName.Enabled = false;
                txtLName.Enabled = false;
                txtMName.Enabled = false;
                ddlCollege.Enabled = false;
                ddlDepartment.Enabled = false;
                ddlPosition.Enabled = false;
                ddlUserLevel.Enabled = false;
                txtBday.Enabled = false;
            }
            catch (Exception)
            {
                
            }


        }


        public void Select()
        {
            string EmployeeID = Request.QueryString["EmployeeID"];

            if (txtEmpNum.Text != EmployeeID)
            {
                txtAddress.Enabled = false;
                txtCancel.Enabled = false;
                txtContactNo.Enabled = false;
                txtEmail.Enabled = false;
                txtEmpNum.Enabled = false;
                txtFName.Enabled = false;
                txtLName.Enabled = false;
                txtMName.Enabled = false;
                ddlCollege.Enabled = false;
                ddlDepartment.Enabled = false;
                ddlPosition.Enabled = false;
                ddlUserLevel.Enabled = false;
                txtBday.Enabled = false;
                txtSubmit.Enabled = false;
               
                SqlCommand cmd12 = new SqlCommand(" SELECT [EmployeeID], [LastName], [FirstName], [MiddleName], [catPositionsID], [CollegeID], [DeptID]," +
                "[UserLevelID], [Address], [Birthday], [ContactNo], [Email], [Password] FROM [formEmployees] WHERE ([EmployeeID] = " + EmployeeID + ")", conn);
                SqlDataAdapter sqlda = new SqlDataAdapter(cmd12);
                DataTable dt12 = new DataTable("dt12");
                sqlda.Fill(dt12);

                txtEmpNum.Text = dt12.Rows[0].ItemArray[0].ToString();
                txtLName.Text = dt12.Rows[0].ItemArray[1].ToString();
                txtFName.Text = dt12.Rows[0].ItemArray[2].ToString();
                txtMName.Text = dt12.Rows[0].ItemArray[3].ToString();
                ddlPosition.SelectedValue = dt12.Rows[0].ItemArray[4].ToString();
                ddlCollege.SelectedValue = dt12.Rows[0].ItemArray[5].ToString();
                ddlDepartment.SelectedValue = dt12.Rows[0].ItemArray[6].ToString();
                ddlUserLevel.SelectedValue = dt12.Rows[0].ItemArray[7].ToString();
                txtAddress.Text = dt12.Rows[0].ItemArray[8].ToString();
                txtBday.Text = dt12.Rows[0].ItemArray[9].ToString();
                txtContactNo.Text = dt12.Rows[0].ItemArray[10].ToString();
                txtEmail.Text = dt12.Rows[0].ItemArray[11].ToString();
                Button1.Enabled = true;
            }
           


        }
        public void Edit()
        {
            
            string EmployeeID = Request.QueryString["EmployeeID"];
            string sqlc = "UPDATE formEmployees SET " +
                "LastName = '" + txtLName.Text +
                "' , FirstName = '" + txtFName.Text +
                "' , MiddleName = '" + txtMName.Text +
                "' , catPositionsID = " + ddlPosition.SelectedValue +
                " , CollegeID = " + ddlCollege.SelectedValue +
                " , DeptID = " + ddlDepartment.SelectedValue +
                " , UserLevelID = " + ddlUserLevel.SelectedValue +
                " , Address = '" + txtAddress.Text +
                "' , Birthday = '" + txtBday.Text +
                "' , ContactNo = " + txtContactNo.Text +
                " , Email = '" + txtEmail.Text +
                "' WHERE(EmployeeID = " + EmployeeID + ")";


            SqlCommand cmd7 = new SqlCommand(sqlc, conn);
            SqlDataAdapter da7 = new SqlDataAdapter(cmd7);
            conn.Open();

            //cmd7.Parameters.AddWithValue("@lname", SqlDbType.VarChar).Value = txtLName.Text;
            //cmd7.Parameters.AddWithValue("@fname", SqlDbType.VarChar).Value = txtFName.Text;
            //cmd7.Parameters.AddWithValue("@mname", SqlDbType.VarChar).Value = txtMName.Text;
            //cmd7.Parameters.AddWithValue("@posID", SqlDbType.Int).Value = ddlPosition.SelectedValue;
            //cmd7.Parameters.AddWithValue("@colID", SqlDbType.Int).Value = ddlCollege.SelectedValue;
            //cmd7.Parameters.AddWithValue("@deptID", SqlDbType.Int).Value = ddlDepartment.SelectedValue;
            //cmd7.Parameters.AddWithValue("@userlevel", SqlDbType.Int).Value = ddlUserLevel.SelectedValue;
            //cmd7.Parameters.AddWithValue("@address", SqlDbType.VarChar).Value = txtAddress.Text;
            //cmd7.Parameters.AddWithValue("@Birthday", SqlDbType.VarChar).Value = txtBday.Text;
            //cmd7.Parameters.AddWithValue("@conNum", SqlDbType.Int).Value = txtContactNo.Text;
            //cmd7.Parameters.AddWithValue("@email", SqlDbType.VarChar).Value = txtEmail.Text;

            cmd7.ExecuteNonQuery();
            conn.Close();
            lblConfirmation.Visible = true;
            lblConfirmation.Text = "You successfully edited employee information";
            


        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            txtAddress.Enabled = true;
            txtCancel.Enabled = true;
            txtContactNo.Enabled = true;
            txtEmail.Enabled = true;
            txtEmpNum.Enabled = true;
            txtFName.Enabled = true;
            txtLName.Enabled = true;
            txtMName.Enabled = true;
            ddlCollege.Enabled = true;
            ddlDepartment.Enabled = true;
            ddlPosition.Enabled = true;
            ddlUserLevel.Enabled = true;
            txtBday.Enabled = true;
            txtSubmit.Enabled = true;
        }

        protected void txtCancel_Click(object sender, EventArgs e)
        {
            txtAddress.Text = "";
            txtContactNo.Text = "";
            txtEmail.Text = "";
            txtEmpNum.Text = "";
            txtFName.Text = "";
            txtLName.Text = "";
            txtMName.Text = "";
        }

    }
}